package com.keyinong.fragment;

import org.apache.http.Header;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.keyinong.restaurant.R;
import com.keyinong.restaurant.ScanActivity;
import com.keyinong.restaurant.entity.DataBean;
import com.keyinong.restaurant.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettlementFragment extends Fragment implements OnClickListener{
	
	private View mview;
	private EditText et_settlementMoney;
	private Button btn_settlement;
	
	@Override
	
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container,  Bundle savedInstanceState) {
		mview=inflater.inflate(R.layout.fragment_settlement_layout,container,false);
		return mview;
	}
	
	@Override
	public void onActivityCreated( Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	private void initView() {
		et_settlementMoney=(EditText)
				mview.findViewById(R.id.et_settlementMoney);
		btn_settlement=(Button)
				mview.findViewById(R.id.btn_settlement);
		btn_settlement.setOnClickListener(this);
		
		
	}
	@Override
	public void onStart() {
		super.onStart();
		et_settlementMoney.setText("");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_settlement:
			getOrderId();
			break;

		}
		
	}

	private void getOrderId() {
		String strMoney=et_settlementMoney.getText().toString().trim();
		double total=0.0;
		if(TextUtils.isEmpty(strMoney)){
			return;
		}
		try {
			total=Double.parseDouble(strMoney);
		} catch (Exception e) {
			Toast.makeText(getActivity(),"请输入正确的金额",
					Toast.LENGTH_SHORT).show();
			return;
		}
		btn_settlement.setBackgroundResource(R.drawable.btn_norecharge);
		btn_settlement.setClickable(false);
		HttpTools httpTools=new HttpTools(getActivity());
		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("token", "");
//		Log.e("lh", "token=="+token+"money=="+total);
		
		httpTools.saveNewOrder(token,""+total,new JsonSaveNewOrder());
		
	}
	private class JsonSaveNewOrder extends JsonHttpResponseHandler{
		
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
//			Log.e("lh", "recharge=="+response.toString());
			btn_settlement.setBackgroundResource(R.drawable.btn_recharge);
			btn_settlement.setClickable(true);
			try{
				Gson gson=new Gson();
				DataBean data=gson.fromJson(response.toString(), 
						DataBean.class);
				if(data.isCode()){
//					et_money.setText("");
//					tv_balance.setText(data.getData());
				Intent intent=new Intent(getActivity(),ScanActivity.class);
				intent.putExtra("orderid", data.getData());
				startActivity(intent);
					
				}else{
					Toast.makeText(getActivity(),data.getMsg(), 
							Toast.LENGTH_SHORT).show();
				}
			}catch(Exception e){
				
			}
		}
		

		
		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			
			super.onFailure(statusCode, headers, responseString, throwable);
			Toast.makeText(getActivity(),"获得结算订单失败", 
			Toast.LENGTH_SHORT).show();
	        btn_settlement.setBackgroundResource(R.drawable.btn_recharge);
	        btn_settlement.setClickable(true);
		}
	}
	

}
